<template>
  <d2-container>
    <template slot="header">器具类别列表</template>
    <el-form :inline="true"  class="demo-form-inline">
      <el-form-item>
        <el-button type="success" size="mini" @click="handleCreate">新增器具类别</el-button>
      </el-form-item>
    </el-form>
    <template>
      <el-table
        :data="tableData"
        style="width: 100%"
        max-height="400">
        <el-table-column
          prop="typeCode"
          label="器具类别代码"
          width="120">
        </el-table-column>
        <el-table-column
          prop="applianceName"
          label="器具类别名称"
          width="120">
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="120">
          <template slot-scope="scope">
            <el-button
              @click.native.prevent="editRow(scope.$index, tableData)"
              type="text"
              size="small">
              编辑
            </el-button>
            <el-button
              @click.native.prevent="deleteRow(scope.$index, tableData)"
              type="text"
              size="small">
              移除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page="pagination.currentPage"
      :page-size="pagination.pageSize"
      :total="pagination.totalPage"
      layout="total, prev, pager, next, jumper">
    </el-pagination>
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      :before-close="handleClose">
      <el-form ref="form" :model="form" label-width="100px">
        <el-col :span="12">
          <el-form-item label="器具类别代码">
            <el-input v-model="form.typeCode" placeholder="" size="mini"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="器具类别名称">
            <el-input v-model="form.applianceName" placeholder="" size="mini"></el-input>
          </el-form-item>
        </el-col>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="handleClose()" size="mini">取 消</el-button>
    <el-button type="primary" @click="handleSubmit()" size="mini">确 定</el-button>
  </span>
    </el-dialog>
  </d2-container>
</template>
<script>
import api from '@/api'

export default {
  name: 'base',
  created () {
    this.getdata()
  },
  methods: {
    async getdata () {
      var res = await api.BASE_UTENSIL_TYPE_LIST({
        pageNo: 1,
        pageSize: 10
      })
      this.pagination.totalPage = res.count
      this.tableData = res.list
    },
    handleCurrentChange (val) {
      this.currentPage = val
      console.log(`当前页: ${val}`)
    },
    contract () {
      this.$router.push({ name: 'market-customer-contract' })
    },
    handleCreate () {
      this.dialogVisible = true
    },
    handleClose () {
      this.dialogVisible = false
      this.initForm()
    },
    handleSubmit () {
      if (this.form.id) {
        api.BASE_UTENSIL_TYPE_EDIT(this.form)
        this.handleClose()
        this.initForm()
        this.getData()
      } else {
        api.BASE_UTENSIL_TYPE_CREATE(this.form).then(() => {
          this.handleClose()
          this.initForm()
          this.getData()
        })
      }
    },
    handleEdit (index, row) {
      this.dialogTitle = '编辑器具类别'
      this.dialogVisible = true
      this.form.departId = row.departId
    },
    handelDelete (index, row) {
      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        api.OA_USER_DELETE()
        this.getData()
      })
    },
    initForm () {
      this.form.departId = 0
    }
  },
  data () {
    return {
      departMentList: [],
      dialogTitle: '新增器具类别',
      dialogVisible: false,
      pagination: {
        currentPage: 1,
        totalPage: 0,
        pageSize: 20
      },
      tableData: [],
      form: {
        applianceId: 0,
        typeCode: '',
        applianceName: ''
      }
    }
  }
}
</script>
